package me.ereach.set;

import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;

public class Queue<T> implements Iterable<T> {
    private T[] queue;
    private int count;

    public Queue(int size) {
        queue = (T[]) new Object[size];
    }

    public void add(T item) {
        if (count >= queue.length)
            throw new IllegalArgumentException("数组超界");

        queue[count++] = item;
    }

    public void remove() {
        if (count > 0)
            queue[--count] = null;
        else
            queue[0] = null;
    }

    @Override
    public Iterator<T> iterator() {
        return new QueueIterator(this);
    }

    private class QueueIterator implements Iterator<T> {
        private Queue<T> list;
        private int index;

        public QueueIterator(Queue<T> list) {
            this.list = list;
        }

        @Override
        public boolean hasNext() {
            return index < list.count;
        }

        @Override
        public T next() {
            return list.queue[index++];
        }
    }

    @Override
    public String toString() {
        return "Queue{" +
                "queue=" + Arrays.toString(queue) +
                '}';
    }
}
